#include <iostream>
#include <set>
#include <string>

using namespace std;


struct Student {
    int age;
    string name;

    /**
     * 先按年龄排，年龄相同再按名字首字母排
     *
     * 1.年龄不同，年龄小的在前
     * 2.年龄相同，名字首字母小的在前
     */
    bool operator<(const Student& other) const {
        if (age != other.age) {
            return age < other.age;
        }
        return name[0] < other.name[0];
    }
};


struct Student2 {
    int age;
    string name;

    /**
     * 先按年龄排，年龄相同再按名字首字母排
     *
     * 1.年龄不同，年龄小的在前
     * 2.年龄相同，名字首字母小的在前
     */
    bool operator<(const Student2& other) const {
        return age < other.age;
    }
};

struct Student3 {
    int age;
    string name;
};
//比较器写在外面
struct Student3Compare{
    bool operator()(const Student3& s1, const Student3& s2) const {
        if (s1.age != s2.age) {
            return s1.age < s2.age; // 年龄小的在前
        }
        return s1.name < s2.name; // 名字首字母小的在前
    }
};



//自定义排序
void Test01() {
    Student s0 = { 20, "Alice" };
	Student s1 = { 20, "Aron" };
	Student s2 = { 20, "Bob" };
	Student s3 = { 18, "Charlie" };
	Student s4 = { 22, "David" };
	Student s5 = { 20, "Eve" };

	set<Student> students;
    students.insert(s0);
	students.insert(s1);
	students.insert(s2);
	students.insert(s3);
	students.insert(s4);
	students.insert(s5);

	for (set<Student>::iterator it = students.begin(); 
        it != students.end(); ++it) {
		cout << "age: " << it->age << ", name: " << it->name << "\n";
	}
}


//自定义排序2
void Test02() {
    Student2 s0 = { 20, "Alice" };
    Student2 s1 = { 20, "Aron" };
    Student2 s2 = { 20, "Bob" };
    Student2 s3 = { 18, "Charlie" };
    Student2 s4 = { 22, "David" };
    Student2 s5 = { 20, "Eve" };

    set<Student2> students;
    students.insert(s0);
    students.insert(s1);
    students.insert(s2);
    students.insert(s3);
    students.insert(s4);
    students.insert(s5);

    for (set<Student2>::iterator it = students.begin();
        it != students.end(); ++it) {
        cout << "age: " << it->age << ", name: " << it->name << "\n";
    }
}

//自定义排序3
//比较器写在外面
void Test03() {
    Student3 s0 = { 20, "Alice" };
    Student3 s1 = { 20, "Aron" };
    Student3 s2 = { 20, "Bob" };
    Student3 s3 = { 18, "Charlie" };
    Student3 s4 = { 22, "David" };
    Student3 s5 = { 20, "Eve" };

    set<Student3, Student3Compare> students;
    students.insert(s0);
    students.insert(s1);
    students.insert(s2);
    students.insert(s3);
    students.insert(s4);
    students.insert(s5);

    for (set<Student3, Student3Compare>::iterator it = students.begin();
        it != students.end(); ++it) {
        cout << "age: " << it->age << ", name: " << it->name << "\n";
    }
}

/**
 * 1. 重载<符号，自定义排序
 * 2. 在外面定义比较器
 */
int main() {
    //Test01();

    Test02();

    return 0;
}